<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link rel="stylesheet" href="/layuimini/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuimini/css/public.css" media="all">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">职务</label>
                            <div class="layui-input-inline">
                                <input type="text" name="position" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
    </div>
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md3">
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
                <legend>组织结构</legend>
            </fieldset>
            <div id="tree" ></div>
        </div>
        <div class="layui-col-md9">
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
                <legend id="organ_name">全部职务</legend>
                <script type="text/html" id="toolbar">
                    <div class="layui-btn-container">
                        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                        <button class="layui-btn layui-btn-sm layui-btn-normal data-delete-btn" lay-event="delete"> 批量删除 </button>
                    </div>
                </script>
                <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter">
                </table>
                <script type="text/html" id="currentTableBar">
                    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
                </script>
            </fieldset>
        </div>
    </div>
    <input type="hidden" id="organId" />
</div>
<script src="/layuimini/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/layuimini/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/layuimini/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script>
    layui.use(['form','table','jquery','util'],function () {
        var $ = layui.jquery;
        var form = layui.form,
            table = layui.table,
            tree = layui.tree,
            layer = layui.layer,
            util = layui.util;

        $.ajax({
            type:'get',
            url:'/organ/tree',
            dataType:'json',
            success:function (res)
            {
                tree.render({
                    elem: '#tree'
                    ,spread:false
                    ,data: res
                    ,id:'id'
                    ,onlyIconControl: true  //是否仅允许节点左侧图标控制展开收缩
                    ,click:getPosition
                   // ,click: function(obj){
                   //     $("#organ_name").text(obj.data.name);
                  //      layer.msg(JSON.stringify(obj.data));
                   // }
                });
            }
        });
        //仅节点左侧图标控制收缩

        function getPosition(obj)
        {
             var result = JSON.stringify(obj.data)
             table.reload('currentTableId', {
                url: '/position/gridList',
                page: {
                    curr: 1
                },
                where: {
                    limit: 10,
                    page: 0,
                    organId:obj.data.id,
                    //searchParams:''
                } //设定异步数据接口的额外参数
            });
            $("#organ_name").text(obj.data.name);
            $("#organId").val(obj.data.id);
        }

        /*渲染数据表格*/
        table.render({
            elem: '#currentTableId',
            url: '/position/grid',
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'position', width: 160, title: '职务'},
                {field: 'description', width: 500, title: '职务描述'},
                {title: '操作', minWidth: 50, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        /*toolbar监听事件*/
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加职务',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/admin/position/add/',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;

                var ids = [];
                for(var i=0;i<data.length;i++){
                    ids.push(data[i].id);
                }
                $.ajax({
                    type:'get',
                    url: '/position/batchDel',
                    data: {'ids':ids.toString()},
                    dataType:'json',
                    success:function(res){
                        layer.alert(JSON.stringify(res));
                        table.reload('currentTableId',{
                            page: {
                                curr: 1
                            }
                        });
                    },
                    error:function(){
                        layer.alert(JSON.stringify('失败'));
                    }
                });
            }
        });

        //操作行
        table.on('tool(currentTableFilter)',function (obj){
            var data = obj.data;
            if (obj.event==='edit')
            {
                layer.msg('修改' + data.id);
                var index = layer.open({
                    title: '职务编辑',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['80%', '80%'],
                    content: '/admin/position/edit/'+data.id,
                });
            }
        });
    });
</script>
</body>